package com.icez.indicator_view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import kotlin.math.max
/**
 * 这个暂时还不建议使用，后面会持续更新
 * @sample 
 * @author Icez
 */
class CircleIndicatorView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : View(context, attrs) {
    private var mNormalRadius:Float = 0f
    private var mSelectedRadius:Float = 0f
    private var mMaxRadius:Float = 0f
    private var mSpace:Int = 9
    private var mNormalWidth:Float = 50f
    private var mSelectedWidth:Float = 80f
    private var mSize:Int = 5
    private val mPaint:Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var mSelectedColor:Int = Color.RED
    private var mNormalColor:Int = Color.GRAY
    private var mSelectedPosition:Int = 1

    init {
        mNormalRadius = mNormalWidth / 2f
        mSelectedRadius = mSelectedWidth / 2f
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if(mSize <= 1){
            return
        }
        mMaxRadius = mSelectedRadius.coerceAtLeast(mNormalRadius)

        val width = (mSize - 1) * mSpace + mNormalWidth * (mSize - 1) + mSelectedWidth
        setMeasuredDimension(width.toInt(),mSelectedWidth.coerceAtLeast(mNormalWidth).toInt())
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if(mSize <= 1){
            return
        }


        var left = 0f
        for(i in 0 until mSize){
            var radius:Float = 0f
            var width:Float = 0f
            if(mSelectedPosition == i){
                mPaint.color = mSelectedColor
                radius = mSelectedRadius
                width = mSelectedWidth
            }else{
                mPaint.color = mNormalColor
                radius = mNormalRadius
                width = mNormalWidth
            }
            canvas?.drawCircle(left+radius,mMaxRadius,radius,mPaint)
            left += width + mSpace
        }
    }
}